package com.zhiyou.servlet;

import com.zhiyou.model.Rent;
import com.zhiyou.service.RentService;
import com.zhiyou.service.impl.RentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class RentAddServlet extends HttpServlet {
    private RentService rS=new RentServiceImpl();
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("添加成功");
        //接受参数封装入rent对象
        Rent rent = new Rent();
        String rhid = req.getParameter("rhid");
        String rlid = req.getParameter("rlid");
        String rprice = req.getParameter("rprice");
        String rpayTime = req.getParameter("rpayTime");
        rent.setRhid(Integer.parseInt(rhid));
        rent.setRlid(Integer.parseInt(rlid));
        rent.setRprice(Double.parseDouble(rprice));
        try {
            rent.setRpayTime(new SimpleDateFormat("yy-MM-dd").parse(rpayTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(rent!=null){
            rS.addNew(rent);

            resp.sendRedirect(req.getContextPath()+"/user/rent?pageNo="+req.getParameter("pageNo"));
        }else{
            req.setAttribute("err","注册失败,请将信息填写完整");
            resp.sendRedirect(req.getContextPath()+"/view/rent/add.jsp");
        }

    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
